/**
 * 绘制分享菜单
 * @param {Object} shareInfo 要分享的内容
 * @param {Function} callback 回调函数
 * @param {String}  shareMethod 分享方法，可选system || uni，决定使用是使用系统分享接口，还是社交平台的分享接口(需要申请平台对应的APPID)
 */
function shareFn(shareInfo, callback, shareMethod = "uni") {
	let drawList = getShareList(shareMethod);
	//以下为计算菜单的nview绘制布局，为固定算法，使用者无关关心
	let screenWidth = plus.screen.resolutionWidth
	//以360px宽度屏幕为例，上下左右边距及2排按钮边距留25像素，图标宽度55像素，同行图标间的间距在360宽的屏幕是30px，但需要动态计算，以此原则计算4列图标分别的left位置
	//图标下的按钮文字距离图标5像素，文字大小12像素
	//底部取消按钮高度固定为44px
	//TODO 未处理横屏和pad，这些情况6个图标应该一排即可
	let marginTop = 25, //上间距
		marginLeft = 25, //左间距
		iconWidth = 55, //图标宽宽
		iconHeight = 55, //图标高度
		icontextSpace = 10, //图标与文字间距
		textHeight = 12 //文字大小
	let left1 = marginLeft / 360 * screenWidth;
	let colNumber = Math.floor(screenWidth / (iconWidth + marginLeft));
	let i = (screenWidth - (iconWidth + marginLeft) * colNumber - marginLeft) / (colNumber + 1);
	let initMargin = marginLeft + i;
	let itemWidth = iconWidth + initMargin;
	let itemHeight = iconHeight + icontextSpace + textHeight + marginTop;
	let textTop = iconHeight + icontextSpace;

	let alphaBg = new plus.nativeObj.View("alphaBg", { //先创建遮罩层
		top: '0px',
		left: '0px',
		height: '100%',
		width: '100%',
		backgroundColor: 'rgba(0,0,0,0.4)' // 背景透明度
	});
	let shareMenu = new plus.nativeObj.View("shareMenu", { //创建底部图标菜单
		bottom: '0px',
		left: '0px',
		height: Math.ceil(drawList.length / colNumber) * itemHeight + marginTop + 44 + 1 + 'px',
		width: '100%',
		backgroundColor: 'rgb(255,255,255)'
	});
	//绘制底部图标菜单的内容
	shareMenu.draw([{
			tag: 'rect', //菜单顶部的分割灰线
			color: '#e7e7e7',
			position: {
				top: '0px',
				height: '1px'
			}
		},
		{
			tag: 'font',
			id: 'sharecancel', //底部取消按钮的文字
			text: '取消分享',
			textStyles: {
				size: '14px'
			},
			position: {
				bottom: '0px',
				height: '44px'
			}
		},
		{
			tag: 'rect', //底部取消按钮的顶部边线
			color: '#e7e7e7',
			position: {
				bottom: '45px',
				height: '1px'
			}
		}
	]);
	drawList.map((v, k) => {
		let time = new Date().getTime();
		let row = Math.floor(k / colNumber);
		let col = k % colNumber;
		let item = [{
			src: v.icon,
			id: Math.random() * 1000 + time,
			tag: "img",
			position: {
				top: row * itemHeight + marginTop,
				left: col * itemWidth + initMargin,
				width: iconWidth,
				height: iconWidth
			}
		}, {
			text: v.text,
			id: Math.random() * 1000 + time,
			tag: "font",
			textStyles: {
				size: textHeight
			},
			position: {
				top: row * itemHeight + textTop,
				left: col * itemWidth + initMargin,
				width: iconWidth,
				height: iconWidth
			}
		}];
		shareMenu.draw(item);
	});

	//绘制完毕之后默认显示出来
	alphaBg.show();
	shareMenu.show();

	//处理遮罩层点击事件
	alphaBg.addEventListener("click", function() {
		console.log("触发遮罩层点击事件");
		alphaBg.hide();
		shareMenu.hide();
	});
	//处理底部图标菜单的点击事件，根据点击位置触发不同的逻辑
	shareMenu.addEventListener("click", function(e) {
		// console.log("触发图标菜单点击事件");
		if (e.screenY > plus.screen.resolutionHeight - 44) { //点击了底部取消按钮
			alphaBg.hide();
			shareMenu.hide();
		} else if (e.clientX < 5 || e.clientX > screenWidth - 5 || e.clientY < 5) {
			//屏幕左右边缘5像素及菜单顶部5像素不处理点击
		} else { //点击了图标按钮
			let x = e.clientX;
			let y = e.clientY;
			let colIdx = Math.floor(x / itemWidth);
			let rowIdx = Math.floor(y / itemHeight);
			let tapIndex = colIdx + rowIdx * colNumber;
			// 回调函数相关的逻辑，可以自定义
			if (callback && typeof callback === 'function') {
				console.log("执行自定义的回调函数");
				callback(tapIndex);
			} else {
				// 当没有传入回调函数时，默认的处理事件
				console.log('点击按钮的序号: ' + tapIndex);
				menuItemClickHandle(tapIndex, shareMethod);
			}
		}
	})
	return {
		alphaBg,
		shareMenu
	};

	/**
	 * @param {Number} tapIndex 点击的分享菜单的索引
	 * @param {Object} options
	 * @param {String} shareMethod 采用的分享方法
	 */
	function menuItemClickHandle(tapIndex, shareMethod) {
		let shareObj = {
			href: shareInfo.href || '',
			title: shareInfo.title || '',
			summary: shareInfo.desc || '',
			success: res => {
				console.log('success:' + JSON.stringify(res));
			},
			fail: err => {
				console.log('fail:' + JSON.stringify(err));
			}
		};
		if (shareMethod === "system") {
			switch (tapIndex) {
				case 0:
					uni.setClipboardData({
						data: shareInfo.desc + shareInfo.href,
						complete() {
							uni.showToast({
								title: '已复制到剪贴板'
							});
						}
					});
					break;
				case 1:
					plus.share.sendWithSystem({
						type: 'web',
						title: shareInfo.title || '',
						thumbs: [shareInfo.imgUrl || './'], // iOS平台下，thumbs提供空字符串会APP崩溃，奇葩
						href: shareInfo.href || '',
						content: shareInfo.desc || ''
					});
					break;
			}
		}
		if (shareMethod === "uni") {
			switch (tapIndex) {
				case 0:
					shareObj.provider = 'weixin';
					shareObj.scene = 'WXSceneSession';
					shareObj.type = 0;
					shareObj.title = shareInfo.title,
					shareObj.imageUrl = shareInfo.imgUrl || '';
					uni.share(shareObj);
					break;
				case 1:
					shareObj.provider = 'weixin';
					shareObj.scene = 'WXSenceTimeline';
					shareObj.type = 0;
					shareObj.imageUrl = shareInfo.imgUrl || '';
					uni.share(shareObj);
					break;
				case 2:
					uni.setClipboardData({
						data: shareInfo.desc + shareInfo.href,
						complete() {
							uni.showToast({
								title: '已复制到剪贴板'
							});
						}
					});
					break;
					// shareObj.provider = 'sinaweibo';
					// shareObj.type = 0;
					// shareObj.imageUrl = shareInfo.imgUrl || '';
					// uni.share(shareObj);
					// break;
				case 3:
					shareObj.provider = 'qq';
					shareObj.type = 1;
					shareObj.imageUrl = shareInfo.imgUrl || '';
					uni.share(shareObj);
					break;
				case 4:
					uni.setClipboardData({
						data: shareInfo.desc + shareInfo.href,
						complete() {
							uni.showToast({
								title: '已复制到剪贴板'
							});
						}
					});
					break;
				case 5:
					plus.share.sendWithSystem({
						type: 'web',
						title: shareInfo.title || '',
						thumbs: [shareInfo.imgUrl || './'], // iOS平台下，thumbs提供空字符串会APP崩溃，奇葩
						href: shareInfo.href || '',
						content: shareInfo.desc || ''
					});
					break;
			}
		}
	}
};

/**
 * 获取绘制的分享列表，可自定义
 * 如果改动了分享的顺序，需要在图标菜单的点击事件监听器中根据点击的索引，进行相应的处理
 * @param {String} shareMethod 分享方法，可选值 system || uni
 */
function getShareList(shareMethod = "system") {
	if (shareMethod === "system") {
		let drawList = [{
				icon: '/static/shareMenu/copy-url.png',
				text: '复制'
			},
			{
				icon: '/static/shareMenu/more.png',
				text: '系统分享'
			}
		];
		return drawList;
	}
	if (shareMethod === "uni") {
		let drawList = [{
				icon: '/static/shareMenu/wechat-1.png',
				text: '微信好友'
			},
			{
				icon: '/static/shareMenu/wechat-2.png',
				text: '朋友圈'
			},
			// {
			// 	icon: '/static/shareMenu/weibo.png',
			// 	text: '微博'
			// },
			// {
			// 	icon: '/static/shareMenu/qq.png',
			// 	text: 'QQ'
			// },
			{
				icon: '/static/shareMenu/copy-url.png',
				text: '复制'
			},
			// {
			// 	icon: '/static/shareMenu/more.png',
			// 	text: '更多'
			// }
		];
		return drawList;
	}
}
export default shareFn;
